Monte Carlo
Simulation Discrete  

By Hubert Ronald / Leave a response / May 28, 2018

Monte Carlo is a discrete simulation method with many practical applications, here how to estimate the pi number.

The area of the circle circumscribed in the square of side 2 and whose equation is \(x^2 + y^2 = 0\) as show in the figure below (radius = 1; center = (0,0)):

It can be calculated like:

The probability that a point falls into circle:

\(P \{(X,Y) \ into \ circle \}\)

\(P \{\ X^2 + Y^2 \leq 1 \} = \frac{\pi}{4}\)

Defined as:

\begin{equation} I= \begin{cases} 1, & \text{if}\ \ X^2 + Y^2 \leq 1 \\ 0, & \text{otherwise} \end{cases} \end{equation}

\( E[I] = P \{\ X^2 + Y^2 \leq 1 \} = \pi/4 \)

Here's implementation in Python:

import random as rand
u = lambda : rand.uniform(-1,1)     # Helper function and parameter
run = 100000                        # Simulation run length

guess = sum([1 for x in range(run) if u()**2 + u()**2 <= 1])

piNumber = 2*2*guess/run            # Clear equation
print ( piNumber )

Aproximately: 3,14...





Hubert Ronald

Hiya. I'm Ronald, an Industrial Engineer from Colombia. Hope you enjoyed my ad-free, bullshit-free site. If you liked it, tell someone about it.

Write a response

Your email address will not be published.

RECENT NEWS

Gradient is a small library which offers a clean, minimalistic but powerful API for gradients on mesh canvas when you use it on Gideros Mobile. This script take colors from uiGradients like inspiration.

More information about before here.

SUBSCRIBE

Get monthly updates and free resources.


CONNECT WITH ME